# 爬取中国天气网的各个城市天气信息
import requests
from lxml import etree

url = "https://www.weather.com.cn/weather1d/101190101.shtml"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}

# 获取页面内容
response = requests.get(url, headers=headers)
response.encoding = "utf-8"
html = response.text

# 解析页面内容
tree = etree.HTML(html)
city_list = tree.xpath('//div[@class="crumbs fl"]/a[3]/text()')
weather_list = tree.xpath("//div[@class='t']/ul/li/p[@class='wea']/text()")
temperature_list = tree.xpath("//div[@class='t']/ul/li/p[@class='tem']/span/text()")
wind_list = tree.xpath("//div[@class='t']/ul/li/p[@class='win']/span/text()")
wind_direction_list = tree.xpath("//div[@class='t']/ul/li/p[@class='win']/span/@title")

for i in range(len(city_list)):
     city = city_list[i]
     weather = weather_list[i]
     temperature = temperature_list[i]
     wind = wind_list[i]
     wind_direction = wind_direction_list[i]
     print(city, weather, temperature, wind, wind_direction)
